# import requests
# from bs4 import BeautifulSoup
# url='https://www.baidu.com/'
# head={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
#
# r=requests.get(url=url,headers=head)
# r.encoding=r.apparent_encoding
# r1= r.text
#
# soup=BeautifulSoup(r1,'lxml')
# a=soup.select('input[id="su"]')[0].attrs['value']
# b=soup.select('#su')[0].attrs['value']
# c=soup.select('input[type="submit"]')[0].attrs['value']
# print(a,b,c)


import requests
from bs4 import BeautifulSoup
import lxml
import csv

def get_html(url):
    head = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'}
    try:
        r = requests.get(url=url, headers=head)
        r.encoding=r.apparent_encoding
        r.raise_for_status()
        return r.text
    except Exception as e:
        print(e)
#解析
def parser(html):
    soup=BeautifulSoup(html,'lxml')
    out_s=[]
    for row in soup.select('table>tbody>tr'):
        td_html=row.select('td')
        row_data=[
            td_html[1].text.strip(),
            td_html[2].text.strip(),
            td_html[3].text.strip(),
            td_html[4].text.strip(),
            td_html[5].text.strip()
        ]
        out_s.append(td_html)
    return out_s
def save_csv(item,path):
    with open(path,'w+',newline=" ",encoding="utf-8")as f:
        csv_write=csv.writer(f)
        csv_write.writerows(item)

if __name__ == '__main__':
    url='http://www.bspider.top/gaosan/'
    yuangma=get_html(url)
    out_list=parser(yuangma)
    p='d//daxue'
    save_csv(p,out_list)